package logic

import (
	"context"
	"errors"
	"user_srv/cache"
	"user_srv/models/mysql"

	"user_srv/internal/svc"
	"user_srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserUpdateAvatarLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserUpdateAvatarLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUpdateAvatarLogic {
	return &UserUpdateAvatarLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserUpdateAvatarLogic) UserUpdateAvatar(in *user.UserUpdateAvatarRequest) (*user.UserUpdateAvatarResponse, error) {
	// todo: add your logic here and delete this line
	var u mysql.User

	err := u.UserMobile(in.Mobile)
	if err != nil {
		logx.Info("检查用户信息错误", err)
		return nil, errors.New("检查用户信息错误")
	}
	err = u.UserUpdateAvatar(in.UserId, in.Avatar)
	if err != nil {
		logx.Info("修改用户头像失败", err)
		return nil, errors.New("修改用户头像失败")
	}
	get := cache.Rdb.Get(l.ctx, "sendSmsupdate"+in.Mobile)
	if get.Val() != in.SendSms {
		logx.Info("获取短信验证码失败,请重新输入", get)
		return nil, errors.New("获取短信验证码失败,请重新输入")
	}
	return &user.UserUpdateAvatarResponse{
		UserId: int64(u.ID),
	}, nil
}
